package com.example.recyclerviewdemo.demo0

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.recyclerviewdemo.Demo0AdapterListener
import com.example.recyclerviewdemo.R
import com.example.recyclerviewdemo.demo0.adapter.BaseQuiAdapter
import com.example.recyclerviewdemo.demo0.adapter.Demo0Adapter
import com.example.recyclerviewdemo.demo0.adapter.Demo0DataBindingAdapter
import com.example.recyclerviewdemo.fillStatus

class Demo0Activity : AppCompatActivity() {

    private val TAG = "Demo0ActivityTag"
    private val demo0ViewModel by viewModels<Demo0ViewModel>()

    //每次拉取数据个数
    companion object{
        const val limitCount = 10
    }

    private lateinit var recyclerView : RecyclerView

    private val demo0Adapter by lazy {
        Demo0Adapter(this).apply {
            setDemo0AdapterListener(object :
                Demo0AdapterListener {
                override fun clickItems(position: Int) {
                    Toast.makeText(this@Demo0Activity, "单击了position:$position", Toast.LENGTH_SHORT)
                        .show()
                }

            })
        }
    }
    private val demo0DataBindingAdapter by lazy { Demo0DataBindingAdapter(this) }
    private val baseQuiAdapter by lazy {
        BaseQuiAdapter(this).apply {

            // loadMoreModule.loadMoreView = CustomLoadMoreView()  //设置自定义的加载更多布局

            //默认底部无数据就会触发，意味着刚进来就会触发
            loadMoreModule.setOnLoadMoreListener {
                Log.d(TAG, "上拉加载更多")
                loadMoreData()
            }
        }
    }

    /**
     * 上拉 加载更多数据
     * */
    private fun loadMoreData() {
        if(baseQuiAdapter.data.size > 30){
            demo0ViewModel.getNormalList(4)
        }else{
            demo0ViewModel.getNormalList(limitCount)
        }


        //获取到数据后，调用对应状态方法
//        baseQuiAdapter.loadMoreModule.loadMoreComplete()
//        baseQuiAdapter.loadMoreModule.loadMoreFail()
//        baseQuiAdapter.loadMoreModule.loadMoreEnd()

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_demo0)
        initViews()
        initRv()
        initObserve()
        initData()

    }

    private fun initViews() {
        recyclerView = findViewById(R.id.recyclerView)
        val normalButton = findViewById<Button>(R.id.normalButton)
        val emptyButton = findViewById<Button>(R.id.emptyButton)
        val errorButton = findViewById<Button>(R.id.errorButton)
//        val refreshLayout = findViewById<SwipeRefreshLayout>(R.id.refreshLayout)

        normalButton.setOnClickListener {
            //如果第一次加载，无数据状态，可以先填充加载中布局
            if(baseQuiAdapter.data.size == 0){
                baseQuiAdapter.fillStatus(hintText= "加载中...")
            }
            demo0ViewModel.getNormalList(limitCount)
        }
        emptyButton.setOnClickListener {
            baseQuiAdapter.setList(mutableListOf())
            demo0ViewModel.getEmptyList()
        }
        errorButton.setOnClickListener {
            baseQuiAdapter.setList(mutableListOf())
            demo0ViewModel.getErrorList()
        }

        //下拉刷新，把数据都清除
    /*    refreshLayout.setOnRefreshListener {
            demo0Adapter.clearList()
            refreshLayout.isRefreshing = false
        }*/
    }

    private fun initObserve() {
        demo0ViewModel.rvList.observe(this, {
            if (it != null) {
                if (it.size > 0) {
                    //正常获取到数据
                    if(it.size < limitCount){
                        Log.d(TAG,"加载更多结束，无更多数据")
                        baseQuiAdapter.loadMoreModule.loadMoreEnd()
                    }else{
                        Log.d(TAG,"加载更多成功")
                        baseQuiAdapter.loadMoreModule.loadMoreComplete()
                    }
                    demo0Adapter.addList(it)
                    baseQuiAdapter.addData(it)
                } else {
                    //如果列表数据是空的，显示空数据界面
                    //if(baseQuiAdapter.itemCount == 0){ //不要这么判断，因为填充的数据也是一个itemCount，包括头尾填充布局也算
                    if(baseQuiAdapter.data.size == 0){
                        Log.d(TAG,"空状态布局")
                        baseQuiAdapter.fillStatus(hintText= "当前暂无数据")
                        //上拉加载更多获取数据的 应该不会走到这里
                    }else{
                        Log.d(TAG,"加载更多结束，无更多数据")
                        baseQuiAdapter.loadMoreModule.loadMoreEnd()
                    }
                }
            } else {
                //如果列表数据是空的，显示错误界面
                if(baseQuiAdapter.data.size == 0){
                    Log.d(TAG,"错误布局")
                    baseQuiAdapter.setEmptyView(R.layout.rv_error)
                }else{
                    Log.d(TAG,"加载更多错误")
                    baseQuiAdapter.loadMoreModule.loadMoreFail()
                }
            }
        })

    }

    private fun initRv() {


        recyclerView.layoutManager = LinearLayoutManager(this)
//        recyclerView.adapter = demo0Adapter
//        recyclerView.adapter = demo0DataBindingAdapter
        recyclerView.adapter = baseQuiAdapter


    }

    private fun initData() {
        //setRvListData()

    }

    /*
    * 获取并设置数据
    * */
    private fun setRvListData() {
        val mutableListOf = mutableListOf<String>()
        for (i in 0..50) {
            mutableListOf.add("item:$i")
        }

        demo0Adapter.setNewList(mutableListOf)
        baseQuiAdapter.setList(mutableListOf)
    }

    //上拉加载更多接口失败

}